<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-form-item label="商品分类" prop="cate_id">
                <el-cascader v-model="form.cate_id" :options="cateList" :props="cateProps" clearable style="width: 100%;"></el-cascader>
            </el-form-item>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '修改分类',
				edit: '选择商品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
            form: {
                id:"",
				cate_id: "",
			},
            cateList:[],
            cateProps: {
				value: "id",
				label: "cate_name",
				checkStrictly: false,
                emitPath: false,
			},
		}
	},
	mounted() {
        this.getCate()  //分类
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.goods.goods.setCate.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		// 表单注入数据
		setData(data) {
            this.form.id = data
		},
        /* 获取分类 */
        async getCate(){
            var res = await this.$API.goods.goodsCate.all.get();
            this.cateList = res.data
        },
	}
}
</script>

<style></style>
